package com.example.bluetooth.commonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class Utils{
	/*
		十六进制转十进制
	*/
	public static long parseHexToDec(String hex){
		if(hex==null||hex.length()==0){
			return -1l;
		}
        long result=0;//最大9223372036854775807
        for(int i=hex.length()-1;i>=0;i--){
            String s=hex.substring(i,i+1);
            if (s.equals("A") || s.equals("a")) {
                result += 10 * Math.pow(16, hex.length() - i - 1);

            } else if (s.equals("B") || s.equals("b")) {
                result += 11 * Math.pow(16, hex.length() - i - 1);

            } else if (s.equals("C") || s.equals("c")) {
                result += 12 * Math.pow(16, hex.length() - i - 1);

            } else if (s.equals("D") || s.equals("d")) {
                result += 13 * Math.pow(16, hex.length() - i - 1);

            } else if (s.equals("E") || s.equals("e")) {
                result += 14 * Math.pow(16, hex.length() - i - 1);

            } else if (s.equals("F") || s.equals("f")) {
                result += 15 * Math.pow(16, hex.length() - i - 1);

            } else {
                result += Integer.parseInt(s) * Math.pow(16, hex.length() - i - 1);

            }

        }
        return result;
    }

    /**
    *十进制转十六进制
    */
    public static String parseDecToHex(String dec){
        if(dec==null||dec.length()==0){
            return "";
        }
        try{
            return Long.toHexString(Long.parseLong(dec));
        }catch(Exception e){
            e.printStackTrace();
            return "";
        }
    }



    /*根据传入的规则格式化数字字符串，整数前补0或截取，小数点后补0或截取*/
    public static String formatNumber(String regix,String value){
        String[] splitR=regix.split("\\.",-1);
        String[] splitV=value.split("\\.",-1);
        String[] tmpV=new String[2];
        tmpV[0]=splitV[0];
        if(splitV.length==1){
            tmpV[1]="";
        }else{
            tmpV[1]=splitV[1];
        }

        if(splitR[0].length()>tmpV[0].length()){
            while(tmpV[0].length()<splitR[0].length()){
                tmpV[0]="0"+tmpV[0];
            }
        }else if(splitR[0].length()<tmpV[0].length()){
            tmpV[0]=tmpV[0].substring(tmpV[0].length()-splitR[0].length(),tmpV[0].length());
        }

        if(splitR.length>1){
                if(splitR[1].length()>tmpV[1].length()){
                    while(tmpV[1].length()<splitR[1].length()){
                        tmpV[1]=tmpV[1]+"0";
                    }
                }else if(splitR[1].length()<tmpV[1].length()){
                    tmpV[1]=tmpV[1].substring(0,splitR[1].length());
                }  
        }

        if(splitR.length==1){
            return tmpV[0];
        }else{
            return tmpV[0]+"."+tmpV[1]; 
        }        
    }

    /**
    *获取7位随机数
    */
    public static String getRandom7(){
        int randomInt=new Random().nextInt(9999999);
        return randomString(formatNumber("0000000",""+randomInt));
    }

    public static String randomString(String org){
        char[] c=org.toCharArray();
        List<Character> lst=new ArrayList<Character>();
        for(int i=0;i<org.length();i++){
            lst.add(c[i]);
        }
        Collections.shuffle(lst);
        String result="";
        for(int i=0;i<org.length();i++){
            result+=lst.get(i);
        }
        return result;
    }

    private static DecimalFormat dfF2H = new DecimalFormat("0");
    public static String parseFloatToHex(String f){
        String format = dfF2H.format(Float.valueOf(f) * 100);
        if(format.contains(",")){
            format=format.replace(",", "");
        }
        return formatNumber("00000000", parseDecToHex(format));
    }

    private static DecimalFormat df = new DecimalFormat("0.00");
    public static String getMoneyFormHex(String hex){
        return df.format((float)parseHexToDec(hex)/100);
    }

    public static String getMoneyFormDec(String dec){
        return df.format(Double.valueOf(dec)/100);
    }

    /*
    *获取校验和 从起始码之后到校验码之前的所有字节之和 去除高位 取后两位
        例如68 02 29 00 00 02 09 09 83 02 26 01 01 00 00 00 00 00 00 00 06 59 14 00 00 08 FC 00 00 00 01 00 00 50 00 00 00 20 00 00 23 25 00 00
        计算02 29 00 00 02 09 09 83 02 26 01 01 00 00 00 00 00 00 00 06 59 14 00 00 08 FC 00 00 00 01 00 00 50 00 00 00 20 00 00 23 25 00 00
        之和为31C 取1C
    */
    public static String getCheckCode(String str){
        int num=0;
        for(int i=0;i<str.length()-2;i+=2){
            num+=Integer.valueOf(str.substring(i,i+2),16);
        }
        String hex=Integer.toHexString(num);
        return hex.substring(hex.length()-2,hex.length()).toUpperCase();
    }
    
    public static String mapToJson(HashMap<String,String> map){
        JSONObject obj=new JSONObject();
        Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, String> next = iterator.next();
            String key=next.getKey();
            String value=next.getValue();
            if(key!=null){
                if(value==null){
                    value="";
                }
                try {
                    obj.put(key,value);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
        return obj.toString();
    }
}